package com.example.demo.controller;

import com.example.demo.api.AddressBookResponse;
import com.example.demo.dto.submit.CreateAddressBookSubmit;
import com.example.demo.dto.submit.PutAddressBookSubmit;
import com.example.demo.dto.vo.PageVo;
import com.example.demo.entity.AddressBook;
import com.example.demo.service.AddressBookService;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 通讯录接口
 *
 * @author wzw
 * @date 2022/4/27 14:53
 */
@Slf4j
@RequiredArgsConstructor
@RestController
public class AddressBookController {
    /**
     * 服务对象
     */
    private final AddressBookService addressBookService;

    /**
     * 创建一条通讯录
     *
     * @param submit        通讯录
     * @param bindingResult 校验结果
     * @return 创建状态
     */
    // todo 2022-05-08 23:40:21 在地址前添加【/】，形成良好习惯
    @PostMapping("/address_book")
    public String createAddressBook(@Valid CreateAddressBookSubmit submit, BindingResult bindingResult) {
        // todo 2022-05-08 23:41:28 每个接口应添加日志，记录必要参数，出现问题时，好还原作案现场，便于排查问题。
        log.info("创建通讯录 -> {}", submit);

        if (bindingResult.hasErrors()) {
            return bindingResult.getAllErrors().get(0).getDefaultMessage();
        }
        return addressBookService.insertAddressBook(submit.getName(), submit.getPhone(), submit.getBirthday(), submit.getRemark());
    }

    /**
     * 修改一条数据
     *
     * @param submit        通讯录
     * @param bindingResult 校验结果
     * @return 修改状态
     */
    @PutMapping("/address_book")
    public String putAddressBook(@Valid PutAddressBookSubmit submit, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.info("修改一条数据 -> {}", submit);
            return bindingResult.getAllErrors().get(0).getDefaultMessage();
        }
        log.info("修改一条数据 -> {}", submit);
        return addressBookService.updateAddressBook(submit.getId(), submit.getName(), submit.getPhone(), submit.getBirthday(), submit.getRemark());
    }

    /**
     * 删除指定的数据
     *
     * @param id 指定id
     * @return 删除状态
     */
    @DeleteMapping("/address_book/{id}")
    public String deleteOneAddressBook(@PathVariable("id") Long id) {
        log.info("获取id -> {}", id);
        return addressBookService.removeAddressBook(id);
    }

    /**
     * 根据id获得通讯录
     *
     * @param id 通讯录id
     * @return 选择的通讯录
     */
    @GetMapping("/address_book/{id}")
    public AddressBookResponse getOneAddressBook(@PathVariable("id") Long id) {
        log.info("获取id -> {}", id);
        AddressBook addressBook = addressBookService.getAddressBookById(id);


        if (addressBook == null) {

            return new AddressBookResponse(addressBook, "查询id不存在", 0);
        }

        return new AddressBookResponse(addressBook, "查询id存在", 1);
    }

    /**
     * 根据page获得通讯录
     *
     * @param pagevo 页面页码
     * @return 选择的通讯录
     */
    @GetMapping("/address_books")
    public AddressBookResponse getAddressBookPages(PageVo pagevo) {

        log.info("获取页面页码 -> {}", pagevo);
        PageInfo<AddressBook> addressBooks = addressBookService.listAddressBooksByPage(pagevo);

        return new AddressBookResponse(addressBooks.getList(), addressBooks.getPageNum(), addressBooks.getPageSize(), addressBooks.getTotal(), addressBooks.getPages());

    }
}